% PLOT AVERAGE DISPLACEMENTS OVER ALL TRIALS

figure(3)
subplot(2,1,1);
plot(FS.time,FS.mean,'-r')       
hold on
plot(FN.time,FN.mean)
plot(target22.X,'-k','LineWidth',1.5)
for i = 1:6
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean displacement over all trials','FontWeight','bold')

subplot(2,1,2);
plot(SS.time,SS.mean,'-r')       
hold on
plot(SN.time,SN.mean)
plot(target18.X,'-k','LineWidth',1.5)
for i = 1:6
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean displacement over all trials','FontWeight','bold')
suplabel(subject,'t')

% PLOT AVERAGE DISPLACEMENTS OVER ALL CYCLES

figure(4)
subplot(2,1,1);
plot(FS.one, '-r')
hold on
plot(FN.one)
plot(target22.one,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean displacement over all cycles','FontWeight','bold')

subplot(2,1,2);
plot(SS.one, '-r')
hold on
plot(SN.one)
plot(target18.one,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Displacement (pixels)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean displacement over all cycles','FontWeight','bold')
suplabel(subject,'t')

% PLOT VELOCITIES INCLUDING SACCADES

figure(5)
subplot(2,1,1);
plot(FS.time,FS.meanvel,'-r')       
hold on
plot(FN.time,FN.meanvel)
plot(target22.Xvel,'-k','LineWidth',1.5)
for i = 1:6
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all trials (incl. saccades: calculated from mean displacement)',...
    'FontWeight','bold');

subplot(2,1,2);
plot(SS.time,SS.meanvel,'-r')       
hold on
plot(SN.time,SN.meanvel)
plot(target18.Xvel,'-k','LineWidth',1.5)
for i = 1:6
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all trials (incl. saccades: calculated from mean displacement)',...
    'FontWeight','bold')
suplabel(subject,'t')

figure(6)
subplot(2,1,1);
plot(FS.onevel, '-r')
hold on
plot(FN.onevel)
plot(target22.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (incl. saccades: calculated from mean displacement)',...
    'FontWeight','bold')

subplot(2,1,2);
plot(SS.onevel, '-r')
hold on
plot(SN.onevel)
plot(target18.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (incl. saccades: calculated from mean displacement)',...
    'FontWeight','bold')
suplabel(subject,'t')

% PLOT VELOCITIES EXCLUDING SACCADES

figure(7)
subplot(2,1,1);
plot(FS.time,FS.meanvelnosac,'-r')       
hold on
plot(FN.time,FN.meanvelnosac)
plot(target22.Xvel,'-k','LineWidth',1.5)
for i = 1:6
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all trials (excluding saccades)','FontWeight','bold')

subplot(2,1,2);
plot(SS.time,SS.meanvelnosac,'-r')       
hold on
plot(SN.time,SN.meanvelnosac)
plot(target18.Xvel,'-k','LineWidth',1.5)
for i = 1:6
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all trials (excluding saccades)','FontWeight','bold')
suplabel(subject,'t')

figure(8)
subplot(2,1,1);
plot(FS.onevelnosac, '-r')
hold on
plot(FN.onevelnosac)
plot(target22.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('22\circ/s Smooth','22\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (excluding saccades)','FontWeight','bold')

subplot(2,1,2);
plot(SS.onevelnosac, '-r')
hold on
plot(SN.onevelnosac)
plot(target18.onevel,'-k','LineWidth',1.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.yvel(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
xlabel('Time (ms)');
ylabel('Velocity (degrees/sec)')
legend('18\circ/s Smooth','18\circ/s Noisy','Location','SouthEast')
title('Mean velocities over all cycles (excluding saccades)','FontWeight','bold')
suplabel(subject,'t')

% PLOT RMSE'S OVERALL AND FOR IN/OUT OF OCCLUDER

Label = {'22 deg/s Noisy';'22 deg/s Smooth';'18 deg/s Noisy';'18 deg/s Smooth'};

figure(9)
bar(Stats.RMSE)
set(gca,'XTickLabel',Label)
xlabel('Trial types')
ylabel('RMS error')
title('RMS errors for whole trials and occluded/visible periods','FontWeight','bold')
legend('RMSE for whole trial','RMSE for occluder',...
    'RMSE for visible target')
suplabel(subject,'t')

% PLOT GAINS

figure(10)
bar(Stats.gain)
set(gca,'XTickLabel',Label)
xlabel('Trial types')
ylabel('Gain (average eye/target velocity)')
title('Gain: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop gain','Resid pred gain (accel)',...
    'Resid pred gain (decel)')
suplabel(subject,'t')

% PLOT MEAN VELOCITY ERRORS

figure(11)
bar(Stats.velerr)
set(gca,'XTickLabel',Label)
xlabel('Trial types')
ylabel('Mean velocity error (target - eye velocity)')
title('Mean velocity error: closed-loop (visible, accel) vs residual predictive (occluded) in accel/deceleration',...
    'FontWeight','bold')
legend('Closed-loop v error','Res pred v error (accel)',...
    'Res pred v error (decel)')
suplabel(subject,'t')

% PLOT LAGS

figure(12)
subplot(2,1,1);
FNbar = bar(target22.time(1:4173,1), FN.lag, 'FaceColor', 'b','EdgeColor', 'b');
hold on
FSbar = bar(target22.time(1:4173,1), FS.lag, 'FaceColor', 'r','EdgeColor', 'r');
xlabel('Time (ms)')
ylabel('Eye position relative to target (pixels)')
plot(target22.X(1:4173,1),'-k','LineWidth',0.5)
for i = 1:2
    patch(target22.plotocc.x(i,:),target22.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
title('Lag of eye behind target','FontWeight','bold')
legend('22\circ/s Noisy','22\circ/s Smooth','Location','SouthEast')

subplot(2,1,2);
SNbar = bar(target18.time(1:5100,1), SN.lag, 'FaceColor', 'b','EdgeColor', 'b');
hold on
SSbar = bar(target22.time(1:5100,1), SS.lag, 'FaceColor', 'r','EdgeColor', 'r');
xlabel('Time (ms)')
ylabel('Eye position relative to target (pixels)')
plot(target18.X(1:5100,1),'-k','LineWidth',0.5)
for i = 1:2
    patch(target18.plotocc.x(i,:),target18.plotocc.y(i,:),[0.9,0.9,0.9]);
    set(patch,'facealpha',0.5)
end
title('Lag of eye behind target','FontWeight','bold')
legend('18\circ/s Noisy','18\circ/s Smooth','Location','SouthEast')
suplabel(subject,'t')